/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.menu.stateDiagram;


import gui.commands.CommandManager;
import gui.commands.stateDiagramCommands.AddConditionalTransitionCommand;
import gui.commands.stateDiagramCommands.AddStateCommand;
import gui.menu.classDiagram.*;
import data.classDiagram.ClassEntity;
import data.DataVector;
import data.classDiagram.Entity;
import data.classDiagram.Trigger;
import data.stateDiagram.ConditionalTransition;
import data.stateDiagram.State;
import data.stateDiagram.Transition;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author SG0217432
 */
public class AddConditionalTransitionForm extends JFrame implements ActionListener {

    private JTextField trigger;
    private JTextField condition;
    private JTextField trueExpression;
    private JTextField falseExpression;
    private JTextField action;
    private JTextField failureAction;
    private JTextField msgs;
    private JTextField failureMsgs;
    private JComboBox  source;
    private JComboBox  destination;
    private JComboBox  failureDestination;
    
    private JButton    addButton;
    private JButton    cancelButton;

    public AddConditionalTransitionForm() {
        super("Add new conditional transition");

        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(0,2));

        contentPane.add(new JLabel("Trigger name "));
        trigger = new JTextField();
        contentPane.add(trigger);
        
        contentPane.add(new JLabel("Condition "));
        condition = new JTextField();
        contentPane.add(condition);
        
        contentPane.add(new JLabel("True expression"));
        trueExpression = new JTextField();
        contentPane.add(trueExpression);
        
        contentPane.add(new JLabel("False expression "));
        falseExpression = new JTextField();
        contentPane.add(falseExpression);
        
        contentPane.add(new JLabel("Action "));
        action = new JTextField();
        contentPane.add(action);
        
        contentPane.add(new JLabel("Failure action "));
        failureAction = new JTextField();
        contentPane.add(failureAction);
        
        contentPane.add(new JLabel("Message "));
        msgs = new JTextField();
        contentPane.add(msgs);
        
        contentPane.add(new JLabel("Failure message "));
        failureMsgs = new JTextField();
        contentPane.add(failureMsgs);
        
        
        source = new JComboBox();       
        destination = new JComboBox();
        failureDestination = new JComboBox();
        
        for (State e : DataVector.getInstance().getActiveStateDiagram().
                getStateDiagram().getStates()){
                source.addItem(e.getName());
                destination.addItem(e.getName());
                failureDestination.addItem(e.getName());
        }
    
        contentPane.add(new JLabel("Source "));
        contentPane.add(source);
        
        contentPane.add(new JLabel("Destination "));
        contentPane.add(destination);
        
        contentPane.add(new JLabel("Failure destination "));
        contentPane.add(failureDestination);
        

        addButton = new JButton("Add");
        addButton.addActionListener(this);
        contentPane.add(addButton);

        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        contentPane.add(cancelButton);
       
        setVisible(true);
        setSize(getPreferredSize());
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();

        if(button == addButton && !trigger.getText().isEmpty() 
                && !condition.getText().isEmpty()) 
        {
	        AddConditionalTransitionCommand command = new AddConditionalTransitionCommand(
			                                                condition.getText(), trueExpression.getText(),
			                                                falseExpression.getText(), action.getText(),
			                                                failureAction.getText(), msgs.getText(),
			                                                failureMsgs.getText(), trigger.getText(),
			                                                source.getSelectedItem().toString(),
			                                                destination.getSelectedItem().toString(),
			                                                failureDestination.getSelectedItem().toString()
	                                                    );

	        CommandManager.getInstance().addAndExecuteStateDiagramCommand(command);
        }

        setVisible(false);
    }
}
